/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager;

import br.com.foxline.clinicplus.entity.*;
import br.com.foxline.clinicplus.enums.CivilStatus;
import br.com.foxline.clinicplus.enums.Gender;
import br.com.foxline.clinicplus.service.AnnexService;
import br.com.foxline.clinicplus.service.CustomerService;
import br.com.foxline.clinicplus.util.Caracter;
import br.com.foxline.clinicplus.util.Msg;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CaptureEvent;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerCustomer implements Serializable {

    @EJB
    private CustomerService customerService;
    @EJB
    private AnnexService annexService;
    private Customer customer;
    private Customer aux;
    private Operator operator;
    private Address address;
    private City city;
    private String url;
    private Annex annex;
    private boolean voltarAtendimentoPaciente;
    private String dateString;

    @PostConstruct
    public void init() {
        this.customer = new Customer();
        this.address = new Address();
        this.city = new City();
        this.operator = new Operator();
        this.annex = new Annex();
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        voltarAtendimentoPaciente = params.get("atendimentopaciente") == null ? false : true;

    }

    public Operator getOperator() {
        return operator;
    }

    public Annex getAnnex() {
        return annex;
    }

    public void setAnnex(Annex annex) {
        this.annex = annex;
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }

    public List<Customer> autocomplete(String query) {
        return customerService.autocomplete(query);
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public List<SelectItem> getGender() {
        List<SelectItem> genders = new ArrayList<SelectItem>();
        for (Gender ms : Gender.values()) {
            genders.add(new SelectItem(ms, ms.getName()));
        }
        return genders;
    }

    public List<SelectItem> selectCivilStatus() {
        List<SelectItem> civilStatus = new ArrayList<SelectItem>();
        for (CivilStatus ms : CivilStatus.values()) {
            civilStatus.add(new SelectItem(ms, ms.getName()));
        }
        return civilStatus;
    }

    public boolean exist() {

        aux = customerService.existCustomer(customer);

        System.err.println(aux);
        if (aux != null) {
            RequestContext context = RequestContext.getCurrentInstance();
            context.update(":customerCreate:customerDlg");
            context.execute("customerDlg.show()");
            return true;
        }
        return false;
    }

    public void loadCustomer() {
        customer = aux;
    }

    public void clearCpf() {
        customer.setCpf("");
        aux = null;
    }

    public void save() {
        customer.setCpf(Caracter.removecaracter(customer.getCpf()));
        address.setCity(city);
        if (annex.getId() != null) {
            customer.setAnnex(annex);
        }
        customer.setOperator(operator);
        customer.setAddress(address);
        customerService.save(customer);

        if (voltarAtendimentoPaciente) {
            Msg.messagemInfoRedirect("Usuário Cadastrado", "atendimentoperfil.xhtml?step=2&customer=" 
                    + customer.getId()+"&date="+dateString);
        }
        if (customer.getId() != null) {
            init();
        }

    }

    private String getRandomImageName() {
        int i = (int) (Math.random() * 10000000);
        return String.valueOf(i);
    }

    public void oncapture(CaptureEvent captureEvent) {
//        String photo = "image-"+getRandomImageName()+"-"+(new Date());
        System.err.println("Salvando");

        annex = new Annex();
        annex.setDescription("AVATAR");
        annex.setFileByte(captureEvent.getData());
        annex.setType("image/jpeg");

        annexService.save(annex);
        System.err.println(annex.getId());


//        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
//        String newFileName = File.separator + "tmp" + File.separator + photo + ".jpg";

        /*
         * FileImageOutputStream imageOutput; try { imageOutput = new
         * FileImageOutputStream(new File(newFileName)); imageOutput.write(data,
         * 0, data.length); imageOutput.close(); url = newFileName; } catch
         * (Exception e) { e.printStackTrace(); throw new FacesException("Error
         * in writing captured image."); }
         */


    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Customer getAux() {
        return aux;
    }

    public void setAux(Customer aux) {
        this.aux = aux;
    }

    public String getDateString() {
        return dateString;
    }

    public void setDateString(String dateString) {
        this.dateString = dateString;
    }
    
}
